<?php
define('HGB_JSAPI','http://192.168.1.199/hgb/user/api/jsapi.php');//限时到账地址
define('HGB_JSID', '5');//接口id
define('HGB_JSKEY', 'JybBsKHJuKNiL8nwRVNvyymGwbYVL6TF');//接口密钥

define('HGB_JSRETURNSITEURL','http://192.168.1.199/weike/lz/');//接口回传地址，本地地址

/**
 * 对称加密
 * @param type $key 密钥
 * @param type $str 明文
 * @return string 密文
 */
function hgb_jsencode($key, $str ){
        $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256,MCRYPT_MODE_ECB),MCRYPT_RAND);
        return mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $str, MCRYPT_MODE_ECB, $iv);
}

/**
 * 对称解密
 * @param type $key 解密
 * @param type $str 密文
 * @return string 明文
 */
function hgb_jsdecode($key, $str ){
        $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256,MCRYPT_MODE_ECB),MCRYPT_RAND);
        return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $str, MCRYPT_MODE_ECB, $iv);
}

/**
 * 与api交互
 * @param array $data 数据
 * @return string
 */
function _hgb_jssendtoapi($data) {
    $data = array('id'=>HGB_JSID, 'content'=>base64_encode(hgb_jsencode(HGB_JSKEY, json_encode($data))));
    $data=http_build_query($data);
    $opts = array(
        'http'=>array(
          'method'=>"POST",
          'header'=>"Content-type:application/x-www-form-urlencoded\r\nContent-Length:".strlen($data)."\r\n",
          'content'=> $data
        )
    );
    $context = stream_context_create($opts);
    return file_get_contents(HGB_JSAPI, FALSE, $context);
}

/**
 * 交易金额范围
 * @return array(min,max)
 */
function hgb_jsmoneyrange() {
    $sendarr = array('op'=>'jsmoneyrange');
    $rstr = _hgb_jssendtoapi($sendarr);
    if ($rstr) {
        return json_decode(trim(hgb_jsdecode(HGB_JSKEY, trim(base64_decode($rstr)))), TRUE);
    }
    return array();
}

/**
 * 查询余额和积分
 * @param int $uid 用户名
 * @return array(umoney,upoints)
 */
function hgb_jsgetumoneyupoints($uid) {
    $sendarr = array('op'=>'jsgetumoneyupoints','uid'=>$uid);
    $rstr = _hgb_jssendtoapi($sendarr);
    if ($rstr) {
        return json_decode(trim(hgb_jsdecode(HGB_JSKEY, base64_decode($rstr))), TRUE);
    }
    return array();
}

/**
 * 是否存在定单号
 * @param string $orderno 定单号
 * @return int
 */
function hgb_jscheckorderno($orderno) {
    $sendarr = array('op'=>'jscheckorderno','orderno'=>$orderno);
    $rstr = _hgb_jssendtoapi($sendarr);
    if ($rstr) {
        return trim(hgb_jsdecode(HGB_JSKEY, trim(base64_decode($rstr))));
    }
    return 0;
}

/**
 * 验证密码
 * @param int $uid 用户中心uid
 * @param string $pwd 密码
 * @return int 成功返回与$uid一致的值
 */
function hgb_jscheckpwd($uid, $pwd) {
    $sendarr = array('op'=>'jscheckpwd','uid'=>$uid,'pwd'=>$pwd);
    $rstr = _hgb_jssendtoapi($sendarr);
    if ($rstr) {
        return trim(hgb_jsdecode(HGB_JSKEY, trim(base64_decode($rstr))));
    }
    return 0;
}

/**
 * 验证密码及没超可用金额
 * @param int $uid 用户中心uid
 * @param string $pwd 密码
 * @param float $money 金额
 * @return int 成功返回与$uid一致的值
 */
function hgb_jscheckpwdmoney($uid, $pwd, $money) {
    $sendarr = array('op'=>'jscheckpwdmoney','uid'=>$uid,'pwd'=>$pwd,'money'=>$money);
    $rstr = _hgb_jssendtoapi($sendarr);
    if ($rstr) {
        return trim(hgb_jsdecode(HGB_JSKEY, trim(base64_decode($rstr))));
    }
    return 0;
}

/**
 * 验证密码及没超可用金额
 * @param int $uid 用户中心uid
 * @param string $pwd 密码
 * @param float $points 金额
 * @return int 成功返回与$uid一致的值
 */
function hgb_jscheckpwdpoints($uid, $pwd, $points) {
    $sendarr = array('op'=>'jscheckpwdpoints','uid'=>$uid,'pwd'=>$pwd,'points'=>$points);
    $rstr = _hgb_jssendtoapi($sendarr);
    if ($rstr) {
        return trim(hgb_jsdecode(HGB_JSKEY, trim(base64_decode($rstr))));
    }
    return 0;
}

/**
 * 验证密码及没超可用金额
 * @param int $uid 用户中心uid
 * @param string $pwd 密码
 * @param float $money 金额
 * @param float $points 积分
 * @return int 成功返回与$uid一致的值
 */
function hgb_jscheckpwdmoneypoints($uid, $pwd, $money, $points) {
    $sendarr = array('op'=>'jscheckpwdmoneypoints','uid'=>$uid,'pwd'=>$pwd,'money'=>$money,'points'=>$points);
    $rstr = _hgb_jssendtoapi($sendarr);
    if ($rstr) {
        return trim(hgb_jsdecode(HGB_JSKEY, trim(base64_decode($rstr))));
    }
    return 0;
}

/**
 * 扣款
 * @param string $name 交易单名称
 * @param string $orderno 定单号
 * @param number $money 金额
 * @param int $uid 支出者id
 * @param string $vcode 识别码
 * @param NULL|TRUE $nocheckorderno 是否检测定单号 NULL不检测 TRUE检测
 * @return int|array(tid,tno,torderno) -1小于限制金额，-2大于限制金额，-3订单号已经存在，-4自己没有激活火工宝,-5余额不足,没有返回创建失败
 */
function hgb_jscreatedebit($name, $orderno, $money, $uid, $vcode, $nocheckorderno=NULL) {
    $sendarr = array('op'=>'jscreatedebit','name'=>$name,'orderno'=>$orderno,'money'=>$money,'uid'=>$uid,'vcode'=>$vcode,'nocheckorderno'=>$nocheckorderno);
    $rstr = _hgb_jssendtoapi($sendarr);
    if ($rstr) {
        return json_decode(trim(hgb_jsdecode(HGB_JSKEY, trim(base64_decode($rstr)))), TRUE);
    }
    return array();
}

/**
 * 扣积分
 * @param string $name 交易单名称
 * @param string $orderno 定单号
 * @param number $points 金额
 * @param int $uid 支出者id
 * @param string $vcode 识别码
 * @param NULL|TRUE $nocheckorderno 是否检测定单号 NULL不检测 TRUE检测
 * @return int|array(tid,tno,torderno) -1小于限制金额，-2大于限制金额，-3订单号已经存在，-4自己没有激活火工宝,-5积分不足,-6超过总收入,没有返回创建失败
 */
function hgb_jsconsumptionpoints($name, $orderno, $points, $uid, $vcode, $nocheckorderno=NULL) {
    $sendarr = array('op'=>'jsconsumptionpoints','name'=>$name,'orderno'=>$orderno,'points'=>$points,'uid'=>$uid,'vcode'=>$vcode,'nocheckorderno'=>$nocheckorderno);
    $rstr = _hgb_jssendtoapi($sendarr);
    if ($rstr) {
        return json_decode(trim(hgb_jsdecode(HGB_JSKEY, trim(base64_decode($rstr)))), TRUE);
    }
    return array();
}

/**
 * 扣款扣积分
 * @param string $name 交易单名称
 * @param string $orderno 定单号
 * @param number $money 金额
 * @param number $points 金额
 * @param int $uid 支出者id
 * @param string $vcode 识别码
 * @param NULL|TRUE $nocheckorderno 是否检测定单号 NULL不检测 TRUE检测
 * @return int|array(tid,tno,torderno) -1小于限制金额，-2大于限制金额，-3订单号已经存在，-4自己没有激活火工宝,-5余额不足,-6积分不足,-7积分超总收入，没有返回创建失败
 */
function hgb_jsconsumptionmoneypoints($name, $orderno, $money, $points, $uid, $vcode, $nocheckorderno=NULL) {
    $sendarr = array('op'=>'jsconsumptionmoneypoints','name'=>$name,'orderno'=>$orderno,'money'=>$money,'points'=>$points,'uid'=>$uid,'vcode'=>$vcode,'nocheckorderno'=>$nocheckorderno);
    $rstr = _hgb_jssendtoapi($sendarr);
    if ($rstr) {
        return json_decode(trim(hgb_jsdecode(HGB_JSKEY, trim(base64_decode($rstr)))), TRUE);
    }
    return array();
}

/**
 * 收入
 * @param string $name 交易单名称
 * @param string $orderno 定单号
 * @param number $money 金额
 * @param int $uid 支出者id
 * @param string $vcode 识别码
 * @param NULL|TRUE $nocheckorderno 是否检测定单号 NULL不检测 TRUE检测
 * @return int|array(tid,tno,torderno) -1小于限制金额，-2大于限制金额，-3订单号已经存在，-4自己没有激活火工宝,-5超过收入,没有返回创建失败
 */
function hgb_jscreateincome($name, $orderno, $money, $uid, $vcode, $nocheckorderno=NULL) {
    if (!$name) {
        $name = '退款/入款';
    }
    $sendarr = array('op'=>'jscreateincome','name'=>$name,'orderno'=>$orderno,'money'=>$money,'uid'=>$uid,'vcode'=>$vcode,'nocheckorderno'=>$nocheckorderno);
    $rstr = _hgb_jssendtoapi($sendarr);
    if ($rstr) {
        return json_decode(trim(hgb_jsdecode(HGB_JSKEY, trim(base64_decode($rstr)))), TRUE);
    }
    return array();
}

/**
 * 加积分
 * @param string $name 交易单名称
 * @param string $orderno 定单号
 * @param number $points 金额
 * @param int $uid 支出者id
 * @param string $vcode 识别码
 * @param NULL|TRUE $nocheckorderno 是否检测定单号 NULL不检测 TRUE检测
 * @return int|array(tid,tno,torderno) -1小于限制金额，-2大于限制金额，-3订单号已经存在，-4自己没有激活火工宝,没有返回创建失败
 */
function hgb_jsaddpoints($name, $orderno, $points, $uid, $vcode, $nocheckorderno=NULL) {
    if (!$name) {
        $name = '加积分';
    }
    $sendarr = array('op'=>'jsaddpoints','name'=>$name,'orderno'=>$orderno,'points'=>$points,'uid'=>$uid,'vcode'=>$vcode,'nocheckorderno'=>$nocheckorderno);
    $rstr = _hgb_jssendtoapi($sendarr);
    if ($rstr) {
        return json_decode(trim(hgb_jsdecode(HGB_JSKEY, trim(base64_decode($rstr)))), TRUE);
    }
    return array();
}

/**
 * 创建交易单
 * @param string $name 交易单名称
 * @param string $orderno 定单号
 * @param number $money 金额
 * @param int $uid 支出者id
 * @param int $touid 接收者id
 * @param string $touname 接收者称呼
 * @param string $vcode 识别码
 * @param NULL|TRUE $nocheckorderno 是否检测定单号 NULL不检测 TRUE检测
 * @param NULL|float $cost 手续费 NULL系统规则扣 float自定义金额
 * @return array|int array(tid,tno,torderno,tstate) -1小于限制金额，-2大于限制金额，-3订单号已经存在，-4自己没有激活火工宝,-5余额不足，-6商家没有激活火工宝,没有返回创建失败
 */
function hgb_jscreattrading($name, $orderno, $money, $uid, $touid, $touname, $vcode, $nocheckorderno=TRUE, $cost=NULL) {
    $sendarr = array('op'=>'jscreatetrading','name'=>$name,'orderno'=>$orderno,'money'=>$money,'uid'=>$uid,'touid'=>$touid,'touname'=>$touname,'vcode'=>$vcode,'nocheckorderno'=>$nocheckorderno,'cost'=>$cost);
    $rstr = _hgb_jssendtoapi($sendarr);
    if ($rstr) {
        return json_decode(trim(hgb_jsdecode(HGB_JSKEY, trim(base64_decode($rstr)))), TRUE);
    }
    return array();
}

/**
 * 测试本地到服务器的连接是否正确
 */
if (isset($_REQUEST['op']) && $_REQUEST['op'] == 'jstestconnettoserver') {
    $sendarr = array('op'=>'jstestconnettoserver','test'=>'test');
    echo trim(hgb_decode(HGB_JSKEY, trim(base64_decode(_hgb_jssendtoapi($sendarr)))));
}

/**
 * 响应服务器到本地的连接测试
 */
if (isset($_REQUEST['id']) && $_REQUEST['id'] == HGB_JSID && isset($_REQUEST['content'])) {
    $rarr = json_decode(trim(hgb_jsdecode(HGB_JSKEY, trim(base64_decode($_REQUEST['content'])))), TRUE);
    if (isset($rarr['test']) && $rarr['test'] == 'test') {
        echo 'success';
    } else {
        echo 'error';
    }
}
